package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.utils.R;
import com.bwie.utils.TokenUtil;
import com.bwie.vo.PageInfoVo;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.ProdInfo;
import com.bwie.mapper.ProdInfoMapper;
import com.bwie.service.ProdInfoService;

import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.SchemaOutputResolver;

/**
 * @Author: 鑫仔
 * @Date: 2025/7/22 09:45
 * @Description:
 **/

@Service
public class ProdInfoServiceImpl extends ServiceImpl<ProdInfoMapper, ProdInfo> implements ProdInfoService {

    @Autowired
    private ProdInfoMapper prodInfoMapper;

    @Autowired
    private HttpServletRequest request;


    @Override
    public R prodInfoList() {
        List<ProdInfo> prodInfoList = list();
        return R.OK(prodInfoList);
    }

    @Override
    public R getProdInfoList(PageInfoVo pageInfoVo) {

        Page<ProdInfo> prodInfoPage = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        LambdaQueryWrapper<ProdInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProdInfo::getProdId, pageInfoVo.getProdId())
                .orderByDesc(ProdInfo::getInfoId);
        Page<ProdInfo> page = prodInfoMapper.selectPage(prodInfoPage, wrapper);
        return R.OK(page);
    }

    @Override
    public R addProdInfo(ProdInfo prodInfo) {
        String token = request.getHeader("token");
        String userName = TokenUtil.getUserName(token);

        prodInfo.setCreateBy(userName);
        prodInfo.setCreateTime(new Date());
        prodInfo.setProdId(prodInfo.getProdId());
        prodInfoMapper.insert(prodInfo);
        return R.OK();
    }

    @Override
    public R updateProdInfo(ProdInfo prodInfo) {
        String token = request.getHeader("token");
        String userName = TokenUtil.getUserName(token);
        prodInfo.setUpdateBy(userName);
        prodInfo.setUpdateTime(new Date());

        prodInfoMapper.updateById(prodInfo);
        return R.OK();
    }

    @Override
    public R deleteProdInfo(ProdInfo prodInfo) {
        prodInfoMapper.deleteById(prodInfo);
        return R.OK();
    }


}
